package serverService;

import java.util.regex.*;

import domain.*;
import serverExceptions.*;

public class MessageParser {

	public static ClientMessage parseClientMessage(String inputString)
			throws ParseException {
		ClientMessage clientMessage = new ClientMessage();
		clientMessage.setMessageType(getMessageType(inputString));

		return clientMessage;
	}

	public static int getExType(String inputString) throws ParseException {
		return parse(inputString, "^.*39=([^\\|]*).*$");
	}
	
	public static MessageType getMessageType(String inputString)
			throws ParseException {
		String regExp = "^.*35=([^\\|]*).*$";
		Pattern pattern = Pattern.compile(regExp);
		java.util.regex.Matcher matcher = pattern.matcher(inputString);
		if (!matcher.matches()) {
			throw new ParseException();
		}
		String cmd = matcher.group(1);
		switch (cmd) {
		case "A":
			return MessageType.LOGON;
		case "5":
			return MessageType.Logout;
		case "8":
			return MessageType.Execution_Report;
		case "3":
			return MessageType.Reject;
		case "D":
			return MessageType.NewOrderSingle;
		case "F":
			return MessageType.OrderCancelRequest;
		}
		return null;
	}

	public static Stock getStock(String inputString)
			throws ParseException {
		String regExp = "^.*55=([^\\|]*).*$";
		Pattern pattern = Pattern.compile(regExp);
		java.util.regex.Matcher matcher = pattern.matcher(inputString);
		if (!matcher.matches()) {
			throw new ParseException();
		}
		String cmd = matcher.group(1);
		switch (cmd) {
		case "BTR":
			return Stock.BTR;
		case "IBM":
			return Stock.IBM;
		case "MSFT":
			return Stock.MSFT;
		case "APPLE":
			return Stock.APPLE;
		}
		return null;
	}
	public static String getSenderCompID(String inputString) throws ParseException {
		String regExp = "^.*49=([^\\|]*).*$";
		Pattern pattern = Pattern.compile(regExp);
		java.util.regex.Matcher matcher = pattern.matcher(inputString);
		if (!matcher.matches()) {
			throw new ParseException();
		}
		String cmd = matcher.group(1);

		return cmd;
	}
	
	public static Integer getOrdID(String inputString) throws ParseException {
		return parse(inputString, "^.*11=([^\\|]*).*$");
	}
	
	

	public static TypeOfOrder getTypeOfOrder(String inputString) throws ParseException {
		switch (parse(inputString, "^.*54=([^\\|]*).*$")) {
		case 1:
			return TypeOfOrder.BUY;
		case 2:
			return TypeOfOrder.SELL;
		} ;
		return null;
	}
	
	public static Integer getOrdQntity(String inputString) throws ParseException {
		return parse(inputString, "^.*38=([^\\|]*).*$");
	}
	public static Integer getSessionRejectReason(String inputString) throws ParseException {
		return parse(inputString, "^.*373=([^\\|]*).*$");
	}
	
	
	
	public static String getText(String inputString) throws ParseException {
		String regExp = "^.*58=([^\\|]*).*$";
		Pattern pattern = Pattern.compile(regExp);
		java.util.regex.Matcher matcher = pattern.matcher(inputString);
		if (!matcher.matches()) {
			throw new ParseException();
		}
		String cmd = matcher.group(1);

		return cmd;
	}
	
	public static Double getPrice(String inputString) throws ParseException {
		String regExp = "^.*44=([^\\|]*).*$";
		Pattern pattern = Pattern.compile(regExp);
		java.util.regex.Matcher matcher = pattern.matcher(inputString);
		if (!matcher.matches()) {
			throw new ParseException();
		}
		String cmd = matcher.group(1);

		return Double.parseDouble(cmd);
	}
	
	
	
	
	public static Integer parse(String inputString, String patt) throws ParseException {
		String regExp = patt;
		Pattern pattern = Pattern.compile(regExp);
		java.util.regex.Matcher matcher = pattern.matcher(inputString);
		if (!matcher.matches()) {
			throw new ParseException();
		}
		String cmd = matcher.group(1);

		return Integer.decode(cmd);
	}
	
	
}
